package com.sikao.app.fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.adapter.MyConcernedAdapter;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.DeleteSubmitBean;
import com.sikao.app.bean.GetListSubmitBean;
import com.sikao.app.bean.MyConcernBean;
import com.sikao.app.dao.CancelAttentionModel;
import com.sikao.app.dao.MyConcernModel;
import com.sikao.app.fragment.base.MyBaseFragment;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.AuctionDetailsActivity;
import com.sikao.app.ui.MallDetailsActivity;
import com.sikao.app.ui.TenderDetailsActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.view.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的关注列表
 */
public class MyConcernedFragment extends MyBaseFragment implements Callback {

	private RefreshListView refreshlistview;
	private View rootView;
	//0卖盘，1买盘,2拍卖
	private int mold;
	private List<MyConcernBean.ResBean.ListBean> beans;
	private MyConcernedAdapter myConcernedAdapter;
	private MyConcernModel myConcernModel;
	private CancelAttentionModel cancelAttentionModel;
	private GetListSubmitBean getListSubmitBean;
	private DeleteSubmitBean deleteSubmitBean;
	private int page = 1;
	private int type;
	private int allPages;
	private int position;
	private View noData, noNetwork;//无数据和无网络布局
	private Intent intent;
	private AlertDialog dialog;

	@Override
	public void notice(final int position) {
		if (cancelAttentionModel == null) {
			deleteSubmitBean = new DeleteSubmitBean();
			cancelAttentionModel = new CancelAttentionModel(getActivity(), new OnNetBackListener<Bean>() {
				@Override
				public void netBack(Bean data) {
					if (data == null) {
						return;
					}
					if (data.getStatus().equals("1")) {
						beans.remove(MyConcernedFragment.this.position);
						myConcernedAdapter.notifyDataSetChanged();
					} else {
						Toast.makeText(getActivity(), data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
			createDialog();
		}
		this.position = position;
		deleteSubmitBean.setId(beans.get(position).getCollect_id());
		dialog.show();
	}

	private void createDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setMessage("确认取消关注吗?");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				cancelAttentionModel.request(JSONUtil.toJSON(deleteSubmitBean), true);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog = builder.create();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		rootView = inflater.inflate(R.layout.fragment_my_concerned, container, false);
		initData();
		initView(rootView);
		initListener();
		init();
		return rootView;
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	private void initData() {
		mold = getArguments().getInt("type");
	}

	private void initView(View view) {
		refreshlistview = (RefreshListView) view.findViewById(R.id.refreshlistview);

	}

	private void initListener() {
		refreshlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (!beans.get(position - 1).getStatus().equals("1")) {
					Toast.makeText(getActivity(), "此商品已不存在", Toast.LENGTH_SHORT).show();
					return;
				}
				if (intent == null) {
					intent = new Intent();
				}
				if (mold == 0) {
					intent.setClass(getActivity(), MallDetailsActivity.class);
					intent.putExtra("goodsId", beans.get(position - 1).getObj_id());
				} else if (mold == 1) {
					intent.setClass(getActivity(), TenderDetailsActivity.class);
					intent.putExtra("inviteId", beans.get(position - 1).getObj_id());
				} else {
					intent.setClass(getActivity(), AuctionDetailsActivity.class);
					intent.putExtra("auctionId", beans.get(position - 1).getObj_id());
				}
				startActivity(intent);
			}
		});
		refreshlistview.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				page = 1;
				type = 1;
				getList(false);
			}

			@Override
			public void onLoadMore() {
				page++;
				type = 2;
				if (allPages < page) {
					Toast.makeText(getActivity(), "无更多内容", Toast.LENGTH_SHORT).show();
					refreshlistview.onLoadMoreComplete();
				} else {
					getList(false);
				}
			}
		});
	}

	private void init() {
		isPrepared = true;
		getListSubmitBean = new GetListSubmitBean();
		myConcernModel = new MyConcernModel(getActivity(), new OnNetBackListener<MyConcernBean>() {
			@Override
			public void netBack(MyConcernBean data) {
				if (type == 1) {
					refreshlistview.onRefreshComplete();
				}
				if (type == 2) {
					refreshlistview.onLoadMoreComplete();
				}
				//无网络
				if (data == null) {
					//清除数据，数据不为空emptyView不会显示
					if (beans.size() != 0) {
						beans.clear();
						myConcernedAdapter.notifyDataSetChanged();
					}
					//如果无数据布局加载过要隐藏掉
					if (noData != null) {
						noData.setVisibility(View.GONE);
					}
					//判断是否加载过无网络布局，避免重复加载显示
					if (noNetwork == null) {
						noNetwork = getActivity().getLayoutInflater().inflate(R.layout.no_network, null);
						((ViewGroup) refreshlistview.getParent()).addView(noNetwork);
						refreshlistview.setEmptyView(noNetwork);
						Button button = (Button) noNetwork.findViewById(R.id.btn_request);
						button.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								type = 0;
								page = 1;
								getList(true);
							}
						});
					} else {
						noNetwork.setVisibility(View.VISIBLE);
					}
					return;
				}
				if (data.getStatus().equals("1")) {
					mHasLoadedOnce = true;
					allPages = data.getRes().getAllPage();
					if (type != 2 && beans.size() != 0) {
						beans.clear();
					}
					beans.addAll(data.getRes().getList());
					myConcernedAdapter.notifyDataSetChanged();
				} else {
					//无数据
					if (data.getStatus().equals("0")) {
						mHasLoadedOnce = true;
						//清除数据，数据不为空emptyView不会显示
						if (beans.size() != 0) {
							beans.clear();
							myConcernedAdapter.notifyDataSetChanged();
						}
						//如果无网络布局加载过要隐藏掉
						if (noNetwork != null) {
							noNetwork.setVisibility(View.GONE);
						}
						//判断是否加载过无数据布局，避免重复加载显示
						if (noData == null) {
							noData = getActivity().getLayoutInflater().inflate(R.layout.content_empty, null);
							((ViewGroup) refreshlistview.getParent()).addView(noData);
							refreshlistview.setEmptyView(noData);
							noData.setOnClickListener(new View.OnClickListener() {
								@Override
								public void onClick(View v) {
									type = 0;
									page = 1;
									getList(true);
								}
							});
						} else {
							noData.setVisibility(View.VISIBLE);
						}

					} else {
						Toast.makeText(getActivity(), data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		beans = new ArrayList<>();
		myConcernedAdapter = new MyConcernedAdapter(getActivity(), this, beans);
		refreshlistview.setAdapter(myConcernedAdapter);

		lazyLoad();
	}

	@Override
	protected void lazyLoad() {
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}
		getList(true);
	}

	private void getList(boolean isShow) {
		getListSubmitBean.setThisPage(page + "");
		getListSubmitBean.setPageNum(HttpApi.LIST_NUMBER);
		if (mold == 0) {
			getListSubmitBean.setType("goods");
		} else if (mold == 1) {
			getListSubmitBean.setType("invite");
		} else {
			getListSubmitBean.setType("auction");
		}
		myConcernModel.request(JSONUtil.toJSON(getListSubmitBean), isShow);
	}
}
